""" prompt
帮我模拟{data}数据组织形式，请优化代码{code} 按照一下要求随机产生与{data}相似的数据组织。
要求
	1. 根节点数据 WGPart1 ... WGPart100
	2. 嵌套子节点 SBB1 ... SBB1000
	3. 节点的数量如果大于1通过元组的形式表达
	4. 节点数量如果为1，则通过字符串表达
	5. 子节点允许嵌套构建
	6. 不允许出现组件本身嵌套本身的情况
	7. 即使组件没有子组件，生成空字典结构

code:


data:
	bom_structure = {
    'WGPart1': {
        'SBB3': {('SBB8', 2), 'SBB4', 'SBB5'},
        'SBB4': {'SBB3': {('SBB8', 2)}, ('SBB9', 2): {}},
        'SBB5': {'SBB1', 'SBB6'},
        'SBB6': {'SBB5', ('SBB3', 10)},
        ('SBB8', 3): {('SBB1', 10)}
    },
    ('WGPart2', 2): {},
    'WGPart3': {'SBB1': {'SBB2'}},
    'WGPart4': {'SBB7': {'SBB8', ('SBB9', 2)}}
}


收集所有的组件名, 模拟结构{code}，字典的key为all_components的字符串，value为随机属性键值对
code：{
    "SBB1": {"大小": "10寸", "材质": "玻璃", "重量": "200g"},
    "SBB2": {"型号": "ABC123", "功耗": "50W"},
    "SBB3": {"品牌": "XYZ", "尺寸": "5mm"},
    "SBB4": {"类型": "机械键盘", "颜色": "黑色", "重量": "300g"},
    "SBB5": {"型号": "Intel Core i7", "功耗": "65W"}
}
"""

import random


def generate_name(prefix, max_num):
    return f"{prefix}{random.randint(1, max_num)}"


def generate_component(sbb_nums):
    component_name = generate_name('SBB', sbb_nums)
    quantity = random.randint(1, 10)
    return (component_name, quantity) if quantity > 1 else component_name


def can_nest(parent, child):
    if isinstance(parent, tuple):
        parent = parent[0]
    if isinstance(child, tuple):
        child = child[0]
    return parent != child


def generate_structure(parent, sbb_nums, depth=1, max_depth=3):
    if depth > max_depth:
        return {}

    children = {}
    num_children = random.randint(1, 5)
    for _ in range(num_children):
        child = generate_component(sbb_nums)
        while True:
            if can_nest(parent, child):
                break
            child = generate_component(sbb_nums)

        if isinstance(child, tuple):
            children[child] = generate_structure(child[0], sbb_nums, depth + 1, max_depth)
        else:
            # 如果没有子结构，生成一个空的字典而不是None
            children[child] = generate_structure(child, sbb_nums, depth + 1, max_depth)

    return children


# Build BOM
def generate_bom_structure(part_nums, sbb_nums):
    # BOM集合
    bom_structure = {}
    num_parts = random.randint(1, part_nums)
    for _ in range(num_parts):
        part_name = generate_name('WGPart', part_nums)
        bom_structure[part_name] = generate_structure(part_name, sbb_nums)

    # 随机决定是否为WGPart添加数量
    for _ in range(random.randint(0, num_parts // 2)):
        part = random.choice(list(bom_structure.keys()))
        if not isinstance(part, tuple):
            quantity = random.randint(2, 10)
            bom_structure[(part, quantity)] = bom_structure.pop(part)

    # 修复None值，如果键值对为('SBB7', 6): None，替换为('SBB7', 6)
    for key, value in list(bom_structure.items()):
        if value is None:
            bom_structure[key] = key  # 假设我们希望用键替换None

    return bom_structure


##---------------------------------------------------


# 收集组件名称的函数
def collect_components(structure, components=None):
    if components is None:
        components = set()

    for key in structure:
        # 添加当前遍历到的组件名称
        if isinstance(key, tuple):
            components.add(key[0])
        else:
            components.add(key)

        # 如果当前组件有子结构，递归遍历子结构
        if isinstance(structure[key], dict):
            collect_components(structure[key], components)

    return components


# 生成随机属性键值对
def generate_random_attributes():
    number_of_attributes = random.randint(1, 3)  # 随机属性的数量
    attributes = {}
    for _ in range(number_of_attributes):
        key = generate_name('Attr', 10)  # 随机生成属性名称
        value = generate_name('Value', 20)  # 随机生成属性值
        attributes[key] = value
    return attributes


# 模拟结构生成函数
def simulate_structure(random_bom_structure):
    # 收集所有节点
    all_components = collect_components(random_bom_structure)
    print("All Components：", all_components)
    print("All Components size：", len(all_components))
    simulated_structure = {}
    for component in all_components:
        simulated_structure[component] = generate_random_attributes()
    return simulated_structure


def genData(part_nums, sbb_nums):
    # 生成并打印随机BOM结构
    random_bom_structure = generate_bom_structure(part_nums, sbb_nums)
    print("BOM Structure：", random_bom_structure)
    # 使用模拟结构生成函数
    simulated_bom_structure = simulate_structure(random_bom_structure)
    print("BOM Attr：", simulated_bom_structure)
    return random_bom_structure, simulated_bom_structure


if __name__ == '__main__':
    genData(part_nums=2, sbb_nums=10)
